#include "Shoppingcar.h"


std::string GetTimeStr(time_t time)
{
    tm *m = localtime(&time);
    char s[1024] = {0};
    sprintf(s," %d年%d月%d日 %d:%d:%d ",m->tm_year + 1900,m->tm_mon + 1,m->tm_mday,m->tm_hour,m->tm_min,m->tm_sec) ;
    return std::string(s);
}


ShoppingCar::~ShoppingCar()
{
    while(recycle.IsEmpty() == false)
    {
        Goods *g = (Goods*)*recycle.GetTop();
        delete g;
        recycle.Pop();

    }
    while(shopping.IsEmpty() == false)
    {
        Goods *g = (Goods*)*shopping.GetFront();
        delete g;
        shopping.Pop();
    }
}

void ShoppingCar::AddGoods()
{
    Goods *g = new Goods;
    std::cout<<"请输入商品名称：";
    std::cin>>g->Name;
    std::cout<<"请输入商品价格：";
    std::cin>>g->Price;
    g->addTime = time(nullptr);//获取当前时间
    std::cout<<std::endl;
    std::cout<<"商品| "<<g->Name<<" |于|"<<GetTimeStr(g->addTime)<<"|加入购物车"<<std::endl;
    shopping.Push(g);


}

void ShoppingCar::DelGoods()
{ 
    std::cout<<"请输入要删除的物品：";
    std::string GoodsName;
    std::cin>>GoodsName;

    LinkQueue tempShop;//临时队列
    while(shopping.IsEmpty() == false)
    {
        Goods *g = (Goods*)*shopping.GetFront();
        shopping.Pop();
        if(g->Name == GoodsName)
        {
            std::cout<<"成功删除物品|"<<g->Name<<"|"<<std::endl;
            g->delTime = time(nullptr);
            recycle.Push(g);
        }
        else
            tempShop.Push(g);
    }
    while(tempShop.IsEmpty() == false)
    {
        shopping.Push(*tempShop.GetFront());
        tempShop.Pop();
    }
}

void ShoppingCar::Undel()
{
    Binshow();
    if(recycle.Size() == 0)
        return;
    std::cout<<"请输入要回复的物品：";
    std::string GoodsName;
    std::cin>>GoodsName;

    LinkStack temprecycle;//临时队列
    while(recycle.IsEmpty() == false)
    {
        Goods *g = (Goods*)*recycle.GetTop();
        recycle.Pop();
        if(g->Name == GoodsName)
        {
            g->addTime = time(nullptr);
            std::cout<<"成功回复物品|"<<g->Name<<"|"<<std::endl;
            shopping.Push(g);
        }
        else
            temprecycle.Push(g);
    }
    while(temprecycle.IsEmpty() == false)
    {
        recycle.Push(*temprecycle.GetTop());
        temprecycle.Pop();
    }
}

void ShoppingCar::show()
{
    std::cout<<"--------购物车清单--------\n";
    LinkQueue tempShop;//临时栈
    while(shopping.IsEmpty() == false)
    {
        tempShop.Push(*shopping.GetFront());
        shopping.Pop();
    }
    while(tempShop.IsEmpty() == false)
    {
        Goods *g = (Goods*)*tempShop.GetFront();
        std::cout<<"物品："<<g->Name<<"   "<<"价格: "<<g->Price<<"   添加时间："<<GetTimeStr(g->addTime)<<std::endl;
        shopping.Push(g);
        tempShop.Pop();
    }
    std::cout<<std::endl;
}

void ShoppingCar::Binshow()
{
    std::cout<<"--------回收站列表--------\n";
    LinkStack temprecycle;//临时栈
    while(recycle.IsEmpty() == false)
    {
        temprecycle.Push(*recycle.GetTop());
        recycle.Pop();
    }
    while(temprecycle.IsEmpty() == false)
    {
        Goods *g = (Goods*)*temprecycle.GetTop();
        std::cout<<"物品："<<g->Name<<"   "<<"价格: "<<g->Price<<"  删除时间："<<GetTimeStr(g->delTime)<<std::endl;
        recycle.Push(g);
        temprecycle.Pop();
    }
    std::cout<<std::endl;
}

void ShoppingCar::delBin()
{
    if(recycle.Size() == 0)
        std::cout<<"回收站为空！"<<std::endl;
    else
    {
        while(recycle.IsEmpty() == false)
        {
            Goods *g = (Goods*)*recycle.GetTop();
            delete g;
            recycle.Pop();

        }
        std::cout<<"回收站已清空！"<<std::endl;
    }
}
